/**
 * 라이브러리 로더
 * 작성자 : 권일룡
 * 작성일 : 2008년 8월 4일
 */
 
package utils
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	
	public class LibraryLoader extends Sprite
	{
		public var bLoad			:Boolean = false;//현재 로딩중인지
		private var loadCount		:int = 0;
		private var load_list		:Array;//로딩리스트
		private var object			:Dictionary;
		private var loader			:Loader;
		private var szDefaultForder	:String;
		
		public function LibraryLoader(szDefaultForder:String)
		{
			object = new Dictionary(true);
			this.szDefaultForder = szDefaultForder;
		}
		
		private function onUrlHandler(e:Event):void
		{
			var filename:String = e.target.loader.contentLoaderInfo.url;
			var array:Array = filename.split("/");
			var clsName:String = array[array.length - 1].split(".")[0];
			var appDomain:ApplicationDomain = e.target.applicationDomain;
			var _class:Class = appDomain.getDefinition(clsName) as Class;

			object[clsName] = _class;
			loadCount++;
			if(loadCount >= load_list.length){
				bLoad = false;
				dispatchEvent(new Event(Event.COMPLETE));
				load_list = null;
				loadCount = 0;			
			}
		}
		
		public function getMovieClip(szName:String):MovieClip{
			if(bLoad) return null;
			if(object[szName] == null) return null;
			return new object[szName]();
		}
		
		public function load(list:Array):void
		{
			if(!bLoad)
			{
				load_list = new Array();
				for each(var name:String in list){
					loader = new Loader();
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onUrlHandler);
					loader.load(new URLRequest(szDefaultForder + name +".swf"));
					load_list.push(loader);
				}
								
			}
		}
		
		public function loadLIB(fName:String, type:int, seq:int):void
		{
			load_list.push(new LoaderElem(fName, type, seq));
		}
	}
}